package com.jevons.geo_edit.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jevons.geo_edit.R;
import com.jevons.geo_edit.geopackage.GisMapGeoManger;
import com.jevons.geo_edit.model.GisOpHistory;
import com.jevons.muffin.util.DateUtils;

import java.util.Date;
import java.util.List;


/**
 * Copyright 2020 JxGIS
 *
 * @file OperationHistoryAdapter
 * @auth linaisheng
 * Created on 2023/6/28.
 * Description：
 */
public class OperationHistoryAdapter extends BaseQuickAdapter<GisOpHistory, BaseViewHolder> {

    private Context mContext;
    private int selectedItemIndex = -1;

    public OperationHistoryAdapter(Context context, List<GisOpHistory> datas) {
        super(R.layout.item_gis_map_op_history, datas);
        this.mContext = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, GisOpHistory item) {
        LinearLayout llRoot = helper.getView(R.id.ll_item_root);
        ImageView tvIcon = helper.getView(R.id.tv_op_icon);
        TextView tvType = helper.getView(R.id.tv_op_type);
        TextView tvTime = helper.getView(R.id.tv_op_time);
        ImageView ivSelected = helper.getView(R.id.iv_selected);

        tvIcon.setImageResource(getOperationFlagToIcon(item.getOperationFlag()));
        tvType.setText(getOperationFlagToText(item.getOperationFlag()));
        if (item.getCreateTime() != 0) {
            tvTime.setText(DateUtils.getTimeInterval(new Date(item.getCreateTime())));
        } else {
            tvTime.setText("-");
        }

        if (helper.getAdapterPosition() == selectedItemIndex) {
            ivSelected.setVisibility(View.VISIBLE);
        } else {
            ivSelected.setVisibility(View.INVISIBLE);
        }

        if (selectedItemIndex != -1 && helper.getAdapterPosition() > selectedItemIndex) {
            tvType.setTextColor(Color.parseColor("#C3C3C3"));
            tvTime.setTextColor(Color.parseColor("#C3C3C3"));
        } else {
            tvType.setTextColor(Color.parseColor("#2A2A2A"));
            tvTime.setTextColor(Color.parseColor("#2A2A2A"));
        }
    }

    private String getOperationFlagToText(String operationFlag) {
        if (GisMapGeoManger.OP_OPEN_PROJECT.equals(operationFlag)) {
            return "打开项目";
        } else if (GisMapGeoManger.OP_ADD_LAYER.equals(operationFlag)) {
            return "新增图层";
        } else if (GisMapGeoManger.OP_EDIT_LAYER.equals(operationFlag)) {
            return "编辑图层";
        } else if (GisMapGeoManger.OP_SORT_LAYER.equals(operationFlag)) {
            return "图层排序";
        } else if (GisMapGeoManger.OP_REMOVE_LAYER.equals(operationFlag)) {
            return "删除图层";
        } else if (GisMapGeoManger.OP_ADD_GEOMETRY.equals(operationFlag)) {
            return "新增图形";
        } else if (GisMapGeoManger.OP_BIANJIE_GEOMETRY.equals(operationFlag)) {
            return "调整节点";
        } else if (GisMapGeoManger.OP_QIEGE_GEOMETRY.equals(operationFlag)) {
            return "图形切割";
        } else if (GisMapGeoManger.OP_WAKONG_GEOMETRY.equals(operationFlag)) {
            return "图形挖孔";
        } else if (GisMapGeoManger.OP_HEBING_GEOMETRY.equals(operationFlag)) {
            return "图形合并";
        } else if (GisMapGeoManger.OP_REMOVE_GEOMETRY.equals(operationFlag)) {
            return "删除图形";
        } else if (GisMapGeoManger.OP_PROPERTY_GEOMETRY.equals(operationFlag)) {
            return "修改属性";
        }  else if (GisMapGeoManger.OP_IMPORT_PATROL_ROUTE.equals(operationFlag)) {
            return "数据导入";
        } else {
            return "-";
        }
    }

    private int getOperationFlagToIcon(String operationFlag) {
        if (GisMapGeoManger.OP_OPEN_PROJECT.equals(operationFlag)) {
            return R.drawable.icon_map_open_project;
        } else if (GisMapGeoManger.OP_ADD_LAYER.equals(operationFlag)) {
            return R.drawable.icon_create_layer;
        } else if (GisMapGeoManger.OP_EDIT_LAYER.equals(operationFlag)) {
            return R.drawable.icon_map_property;
        } else if (GisMapGeoManger.OP_SORT_LAYER.equals(operationFlag)) {
            return R.drawable.icon_map_sort;
        } else if (GisMapGeoManger.OP_REMOVE_LAYER.equals(operationFlag)) {
            return R.drawable.icon_remove_layer;
        } else if (GisMapGeoManger.OP_ADD_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_huizhi;
        } else if (GisMapGeoManger.OP_BIANJIE_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_bianjie;
        } else if (GisMapGeoManger.OP_QIEGE_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_qiege;
        } else if (GisMapGeoManger.OP_WAKONG_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_wakong;
        } else if (GisMapGeoManger.OP_HEBING_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_hebing;
        } else if (GisMapGeoManger.OP_REMOVE_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_shanchu;
        } else if (GisMapGeoManger.OP_PROPERTY_GEOMETRY.equals(operationFlag)) {
            return R.drawable.icon_map_property;
        } else {
            return R.drawable.icon_map_more;
        }
    }


    public void notifySelectedItem(String historyId) {
        if (historyId == null) {
            return;
        }
        for (int i = 0; i < getItemCount(); i++) {
            if (historyId.equals(getItem(i).getId())) {
                selectedItemIndex = i;
                notifyDataSetChanged();
                break;
            }
        }
        if (selectedItemIndex == getItemCount() -1) {
            getRecyclerView().scrollToPosition(selectedItemIndex);
        }
    }
}